﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace VietKinsNutrition.Data
{
    public class ClsTblDm_HoadonnhaptrongngayDAL : ClsConnection
    {
        //PrcTblDm_Hoadondicho
        static string sNamhocId;
        public ClsTblDm_HoadonnhaptrongngayDAL()
        {
            ClsTblDm_Namhocdal = new ClsTblDm_NamhocDAL();
            sNamhocId = ClsTblDm_Namhocdal.ReturnNamhocId().ToString();
        }
        ClsTblDm_NamhocDAL ClsTblDm_Namhocdal;
        private string ReturnSDate(DateTime Value)
        {
            string sReturn;
            string[] Para = Value.ToShortDateString().Split('/');
            if (Para[0].Length == 1)
            {
                Para[0] = "0" + Para[0].ToString();
            }
            if (Para[1].Length == 1)
            {
                Para[1] = "0" + Para[1].ToString();
            }
            sReturn = Para[0].ToString() + "/" + Para[1].ToString() + "/" + Para[2].ToString();
            return sReturn;
        }
        //public void a()
        //{ 
        // SP_REPORT_SOTONKHO = "\t     SELECT                   (SELECT Tenthucan FROM TblThucpham_thucan WHERE (Id = vwTP.Thucpham_ThucanId) OR (Id = vwKho.Thucpham_ThucanId)) AS Tenthucan,   \t\t\t(SELECT  TblDm_Donvitinh.Tendonvitinh FROM TblThucpham_thucan AS tp1 INNER JOIN TblDm_Donvitinh ON tp1.DonvitinhId = TblDm_Donvitinh.Id  WHERE (tp1.Id = vwTP.Thucpham_ThucanId) OR (tp1.Id = vwKho.Thucpham_ThucanId)) as Tendonvitinh,  \t\t\t\tvwKho.Soluong as SLTon,                            (SELECT     TOP (1) PERCENT Dongia                              FROM          vwNhaponDay AS nhap1                              WHERE      (CONVERT(nvarchar(10), NgayNX, 103) < CONVERT(nvarchar(10), @dateReport, 103)) AND (Thucpham_ThucanId = vwKho.Thucpham_ThucanId)                              ORDER BY NgayNX DESC) AS GiaTon,   \t\t\ttblNhap.Soluong AS SLNhap, tblNhap.Dongia AS GiaNhap, tblXuat.Soluong AS SLXuat, tblXuat.Dongia AS GiaXuat  FROM     (SELECT NgayNX, Thucpham_ThucanId FROM vwReportThucpham WHERE (CONVERT(nvarchar(10), NgayNX, 103) = CONVERT(nvarchar(10), @dateReport, 103))) AS vwTP LEFT OUTER JOIN           (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwXuatonDay WHERE (CONVERT(nvarchar(10), NgayNX, 103) = CONVERT(nvarchar(10), @dateReport, 103))) AS tblXuat ON vwTP.Thucpham_ThucanId = tblXuat.Thucpham_ThucanId LEFT OUTER JOIN           (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwNhaponDay WHERE (CONVERT(nvarchar(10), NgayNX, 103) = CONVERT(nvarchar(10), @dateReport, 103))) AS tblNhap ON vwTP.Thucpham_ThucanId = tblNhap.Thucpham_ThucanId FULL OUTER JOIN           (SELECT Thucpham_ThucanId, SUM(Soluong) AS Soluong FROM vwKhobyDay WHERE (CONVERT(nvarchar(10), NgayNX, 103) < CONVERT(nvarchar(10), @dateReport, 103)) GROUP BY Thucpham_ThucanId HAVING  (SUM(Soluong) > 0)) AS vwKho ON vwTP.Thucpham_ThucanId = vwKho.Thucpham_ThucanId  ORDER BY Tenthucan ";

        //}
        public DataTable GetTable_Hoadondicho(DateTime NgayNX)
        {
            OleDbConnection cn = ClsConnection.OleDBConnect();

            DataTable table = new DataTable();
            DataColumn Columm = new DataColumn();
            Columm.ColumnName = Columm.Caption = "STT";
            Columm.DataType = System.Type.GetType("System.Int32");
            Columm.AllowDBNull = false;
            Columm.AutoIncrement = true;
            Columm.AutoIncrementSeed = Columm.AutoIncrementStep = 1;
            table.Columns.Add(Columm);

            //QueryString.SP_REPORT_SOTONKHO = "	"
            //   + "     SELECT (SELECT Tenthucan FROM TblThucpham_thucan WHERE (Id = vwTP.Thucpham_ThucanId) OR (Id = vwKho.Thucpham_ThucanId)) AS Tenthucan,  "
            //   + " 			(SELECT  TblDm_Donvitinh.Tendonvitinh FROM TblThucpham_thucan AS tp1 INNER JOIN TblDm_Donvitinh ON tp1.DonvitinhId = TblDm_Donvitinh.Id  WHERE (tp1.Id = vwTP.Thucpham_ThucanId) OR (tp1.Id = vwKho.Thucpham_ThucanId)) as Tendonvitinh, "
            //   + "            vwKho.SL as SLTon, "
            //   + "            (SELECT TOP 1 Dongia FROM vwNhaponDay AS nhap1 WHERE (nhap1.NgayNX < #" + NgayNX.AddDays(1).ToShortDateString() + "#) AND (nhap1.Thucpham_ThucanId = vwKho.Thucpham_ThucanId) ORDER BY NgayNX DESC) AS GiaTon,  "
            //   + " 			tblNhap.Soluong AS SLNhap, tblNhap.Dongia AS GiaNhap, tblXuat.Soluong AS SLXuat, tblXuat.Dongia AS GiaXuat "
            //   + " FROM (((SELECT NgayNX, Thucpham_ThucanId FROM vwReportThucpham WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#)) AS vwTP LEFT OUTER JOIN "
            //   + "        (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwXuatonDay WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#)) AS tblXuat ON vwTP.Thucpham_ThucanId = tblXuat.Thucpham_ThucanId) LEFT OUTER JOIN "
            //   + "        (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwNhaponDay WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#)) AS tblNhap ON vwTP.Thucpham_ThucanId = tblNhap.Thucpham_ThucanId) LEFT OUTER JOIN "
            //   + "        (SELECT Thucpham_ThucanId, SUM(Soluong) AS SL FROM vwKhobyDay WHERE (NgayNX < #" + NgayNX.ToShortDateString() + "#) GROUP BY Thucpham_ThucanId HAVING  (SUM(Soluong) > 0)) AS vwKho ON vwTP.Thucpham_ThucanId = vwKho.Thucpham_ThucanId "
            //   + " union "
            //   + "     SELECT (SELECT Tenthucan FROM TblThucpham_thucan WHERE (Id = vwTP.Thucpham_ThucanId) OR (Id = vwKho.Thucpham_ThucanId)) AS Tenthucan,  "
            //   + " 			(SELECT  TblDm_Donvitinh.Tendonvitinh FROM TblThucpham_thucan AS tp1 INNER JOIN TblDm_Donvitinh ON tp1.DonvitinhId = TblDm_Donvitinh.Id  WHERE (tp1.Id = vwTP.Thucpham_ThucanId) OR (tp1.Id = vwKho.Thucpham_ThucanId)) as Tendonvitinh, "
            //   + "            vwKho.SL as SLTon, "
            //   + "            (SELECT TOP 1 Dongia FROM vwNhaponDay AS nhap1 WHERE (nhap1.NgayNX < #" + NgayNX.AddDays(1).ToShortDateString() + "#) AND (nhap1.Thucpham_ThucanId = vwKho.Thucpham_ThucanId) ORDER BY NgayNX DESC) AS GiaTon,  "
            //   + " 			tblNhap.Soluong AS SLNhap, tblNhap.Dongia AS GiaNhap, tblXuat.Soluong AS SLXuat, tblXuat.Dongia AS GiaXuat "
            //   + " FROM (((SELECT NgayNX, Thucpham_ThucanId FROM vwReportThucpham WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#)) AS vwTP LEFT OUTER JOIN "
            //   + "        (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwXuatonDay WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#)) AS tblXuat ON vwTP.Thucpham_ThucanId = tblXuat.Thucpham_ThucanId) LEFT OUTER JOIN "
            //   + "        (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwNhaponDay WHERE (NgayNX BETWEEN  #" + NgayNX + "# and #" + NgayNX.AddDays(1) + "#)) AS tblNhap ON vwTP.Thucpham_ThucanId = tblNhap.Thucpham_ThucanId) right OUTER JOIN "
            //   + "        (SELECT Thucpham_ThucanId, SUM(Soluong) AS SL FROM vwKhobyDay WHERE (NgayNX < #" + NgayNX.ToShortDateString() + "#) GROUP BY Thucpham_ThucanId HAVING  (SUM(Soluong) > 0)) AS vwKho ON vwTP.Thucpham_ThucanId = vwKho.Thucpham_ThucanId ";




            //  iif(ISNULL(TblDm_Nhucaudinhduongotruong.KCaloCd), 0)
            QueryString.SP_REPORT_SOTONKHO = " " +
            " select DISTINCT  * from (SELECT  TG2.Tenthucan, TG2.Tendonvitinh, iif(ISNULL(TG2.slnhap),0,TG2.slnhap) AS slnhap ,  iif(ISNULL(TG2.gianhap),0,TG2.gianhap) AS gianhap,  iif(ISNULL(TG2.slxuat),0,TG2.slxuat) AS slxuat, iif(ISNULL(TG2.giaxuat),0,TG2.giaxuat) AS giaxuat,  iif(ISNULL(Tbl_Hangton.slTon),0,Tbl_Hangton.slTon) AS slTon, iif(ISNULL(Tbl_Hangton.slTon),0,TG2.gianhap) AS GiaTon " +
 " FROM ( SELECT Sum(TBlTrunggian.Soluong) AS slTon, TBlTrunggian.Thucpham_ThucanId FROM (select   Dongia, Soluong, Thucpham_ThucanId from " +
" (SELECT     Dongia, Soluong, Thucpham_ThucanId   FROM   View_Nhaphanghoa where(NgayNX < #" + NgayNX.AddDays(1).ToShortDateString() + "#)    UNION    SELECT     Dongia, Soluong, Thucpham_ThucanId   FROM  View_Xuathanghoa where(NgayNX < #" + NgayNX.AddDays(1).ToShortDateString() + "#)) TonNhapxuathanghoa)  TBlTrunggian " +
" GROUP BY TBlTrunggian.Thucpham_ThucanId HAVING (Sum(TBlTrunggian.Soluong)<>0))  Tbl_Hangton RIGHT JOIN (SELECT tblNhap.Thucpham_ThucanId, tblNhap.NgayNX, tblNhap.Tenthucan, tblNhap.Tendonvitinh, tblNhap.Soluong AS slnhap, tblNhap.Dongia AS gianhap, TG1.slxuat, TG1.giaxuat " +
" FROM (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwNhaponDay WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#))  tblNhap LEFT JOIN (SELECT vwTP.Thucpham_ThucanId AS Thucpham_ThucanId, vwTP.NgayNX AS TBlXuat_NgayNX, tblXuat.Tenthucan, tblXuat.Soluong AS slxuat, tblXuat.Tendonvitinh, tblXuat.Dongia AS giaxuat, tblXuat.Trangthai " +
" FROM (SELECT NgayNX, Thucpham_ThucanId FROM vwReportThucpham WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#) )  vwTP LEFT JOIN (SELECT Tenthucan, Soluong, Tendonvitinh, Dongia, Thanhtien, NgayNX, Trangthai, Thucpham_ThucanId FROM vwXuatonDay WHERE (NgayNX BETWEEN  #" + NgayNX.ToShortDateString() + "# and #" + NgayNX.AddDays(1).ToShortDateString() + "#))  tblXuat ON vwTP.Thucpham_ThucanId = tblXuat.Thucpham_ThucanId)  TG1 ON tblNhap.Thucpham_ThucanId = TG1.Thucpham_ThucanId)  TG2 ON Tbl_Hangton.Thucpham_ThucanId = TG2.Thucpham_ThucanId)  Thomnd ";

            // + " ORDER BY Tenthucan ";
            OleDbCommand comd = new OleDbCommand(QueryString.SP_REPORT_SOTONKHO, cn);
            //comd.CommandType = CommandType.StoredProcedure;
            //comd.Parameters.Add(new OleDbParameter("@dateReport", NgayNX));
            try
            {
                comd.ExecuteNonQuery();
                OleDbDataAdapter Adapter = new OleDbDataAdapter(comd);
                Adapter.Fill(table);
                cn.Close();
            }
            catch (System.Exception ex)
            {
            }
            return table;
        }

        public DataTable GetTable_HoadondichoByThomnd(DateTime NgayNX)
        {
            OleDbConnection cn = ClsConnection.OleDBConnect();

            DataTable Tbl_Nhap = new DataTable("TblNhap");
            DataTable Tbl_Xuat = new DataTable("Tbl_Xuat");
            DataTable Tbl_Ton = new DataTable("Tbl_Ton");

            DataTable table = new DataTable();
            DataColumn STTColumm = new DataColumn();
            STTColumm.ColumnName = STTColumm.Caption = "STT";
            STTColumm.DataType = System.Type.GetType("System.Int32");
            STTColumm.AllowDBNull = false;
            STTColumm.AutoIncrement = true;
            STTColumm.AutoIncrementSeed = STTColumm.AutoIncrementStep = 1;
            Tbl_Nhap.Columns.Add(STTColumm);



            DataColumn SlTonColumm = new DataColumn();
            SlTonColumm.ColumnName = SlTonColumm.Caption = "SLTon";
            SlTonColumm.DataType = System.Type.GetType("System.Double");
            Tbl_Nhap.Columns.Add(SlTonColumm);

            DataColumn GiaTonColumm = new DataColumn();
            GiaTonColumm.ColumnName = GiaTonColumm.Caption = "GiaTon";
            GiaTonColumm.DataType = System.Type.GetType("System.Double");
            Tbl_Nhap.Columns.Add(GiaTonColumm);



            DataColumn SlXuatColumm = new DataColumn();
            SlXuatColumm.ColumnName = SlXuatColumm.Caption = "SLXuat";
            SlXuatColumm.DataType = System.Type.GetType("System.Double");
            Tbl_Nhap.Columns.Add(SlXuatColumm);

            DataColumn GiaXuatColumm = new DataColumn();
            GiaXuatColumm.ColumnName = GiaXuatColumm.Caption = "Giaxuat";
            GiaXuatColumm.DataType = System.Type.GetType("System.Double");
            Tbl_Nhap.Columns.Add(GiaXuatColumm);



            string SqlThucpham_TontruocByNgay_Giakho =

                 " SELECT     View_Tonkho.Thucpham_ThucanId ,TblThucpham_thucan.Tenthucan, Round(View_Tonkho.Soluong,10) AS SlTon, (SELECT top 1 TblNhapxuathanghoa.Dongia " +
" FROM TblHoadonnhapxuat INNER JOIN TblNhapxuathanghoa ON TblHoadonnhapxuat.Id = TblNhapxuathanghoa.HoadonnhapxuatId " +
 " WHERE TblHoadonnhapxuat.Trangthai=-1 AND TblHoadonnhapxuat.NgayNX< #" + NgayNX.ToString("MM/dd/yyyy") + "# AND TblHoadonnhapxuat.NamhocId =" + sNamhocId + " and TblNhapxuathanghoa.Thucpham_ThucanId= View_Tonkho.Thucpham_ThucanId  ORDER BY TblNhapxuathanghoa.HoadonnhapxuatId DESC , TblHoadonnhapxuat.NgayNX DESC ) AS Giaton,  " +
                      " TblDm_Donvitinh.Tendonvitinh FROM        ( TblThucpham_thucan INNER JOIN " +
                     " TblDm_Donvitinh ON TblThucpham_thucan.DonvitinhId = TblDm_Donvitinh.Id) INNER JOIN ( " +
" SELECT     SUM(TblNhapxuathanghoa.Soluong) AS Soluong, TblNhapxuathanghoa.Thucpham_ThucanId " +
    " FROM   ( SELECT     Dongia, Soluong, Thucpham_ThucanId, NgayNX " +
           " FROM          View_Nhaphanghoa WHERE      (NgayNX < #" + NgayNX.ToString("MM/dd/yyyy") + "#) UNION ALL SELECT     Dongia, Soluong, Thucpham_ThucanId, NgayNX FROM        View_Xuathanghoa "
+ " WHERE     (NgayNX <#" + NgayNX.ToString("MM/dd/yyyy") + "#) ) AS TblNhapxuathanghoa GROUP BY TblNhapxuathanghoa.Thucpham_ThucanId HAVING      (SUM(TblNhapxuathanghoa.Soluong) > 0) " +
" ) AS View_Tonkho ON TblThucpham_thucan.Id = View_Tonkho.Thucpham_ThucanId  ";

            string SqlThucpham_XuatByNgay = "SELECT TblNhapxuathanghoa.Thucpham_ThucanId,(Select Tenthucan from TblThucpham_thucan where Id = TblNhapxuathanghoa.Thucpham_thucanId)AS Tenthucan, (Select Tendonvitinh from TblDm_Donvitinh where Id = TblNhapxuathanghoa.DonvitinhId) AS Tendonvitinh ,  Round(Sum(TblNhapxuathanghoa.Soluong),10) AS SlXuat, TblNhapxuathanghoa.Dongia AS GiaXuat " +
" FROM TblNhapxuathanghoa INNER JOIN TblHoadonnhapxuat ON TblNhapxuathanghoa.HoadonnhapxuatId = TblHoadonnhapxuat.Id " +
" GROUP BY TblNhapxuathanghoa.Thucpham_ThucanId, TblNhapxuathanghoa.DonvitinhId,TblNhapxuathanghoa.Dongia, TblHoadonnhapxuat.NamhocId, TblHoadonnhapxuat.NgayNX, TblHoadonnhapxuat.Trangthai" +
" HAVING TblHoadonnhapxuat.NamhocId=" + sNamhocId + " AND TblHoadonnhapxuat.NgayNX=#" + NgayNX.ToString("MM/dd/yyyy") + "# AND TblHoadonnhapxuat.Trangthai=0";

            string SqlThucpham_NhapByNgay = " SELECT TblNhapxuathanghoa.Thucpham_ThucanId,(Select Tenthucan from TblThucpham_thucan where Id = TblNhapxuathanghoa.Thucpham_thucanId)AS Tenthucan, (Select Tendonvitinh from TblDm_Donvitinh where Id = TblNhapxuathanghoa.DonvitinhId) AS Tendonvitinh,Round(Sum(TblNhapxuathanghoa.Soluong),10) AS SLNhap, TblNhapxuathanghoa.Dongia AS GiaNhap   " +
" FROM TblNhapxuathanghoa INNER JOIN TblHoadonnhapxuat ON TblNhapxuathanghoa.HoadonnhapxuatId = TblHoadonnhapxuat.Id " +
" GROUP BY TblNhapxuathanghoa.Thucpham_ThucanId, DonvitinhId ,  TblNhapxuathanghoa.Dongia, TblHoadonnhapxuat.NamhocId, TblHoadonnhapxuat.Trangthai, TblHoadonnhapxuat.NgayNX " +
" HAVING TblHoadonnhapxuat.NamhocId=" + sNamhocId + " AND TblHoadonnhapxuat.Trangthai=-1 AND TblHoadonnhapxuat.NgayNX=#" + NgayNX.ToString("MM/dd/yyyy") + "#";

            // + " ORDER BY Tenthucan ";
            OleDbCommand comd = new OleDbCommand();
            try
            {
                comd.Connection = cn;
                comd.CommandText = SqlThucpham_NhapByNgay;
                comd.ExecuteNonQuery();
                OleDbDataAdapter Adapter = new OleDbDataAdapter(comd);
                Adapter.Fill(Tbl_Nhap);

                comd.CommandText = SqlThucpham_XuatByNgay;
                Adapter = new OleDbDataAdapter(comd);
                comd.ExecuteNonQuery();
                Adapter.Fill(Tbl_Xuat);

                comd.CommandText = SqlThucpham_TontruocByNgay_Giakho;
                Adapter = new OleDbDataAdapter(comd);
                comd.ExecuteNonQuery();
                Adapter.Fill(Tbl_Ton);

                cn.Close();
            }
            catch (System.Exception ex)
            {

            }

            try
            {

                // su ly xuat vao nhap

                for (int iNhap = 0; iNhap < Tbl_Nhap.Rows.Count; iNhap++)
                {
                    for (int iXuat = 0; iXuat < Tbl_Xuat.Rows.Count; iXuat++)
                    {
                        if (Tbl_Nhap.Rows[iNhap]["Thucpham_thucanId"].ToString().Trim() == Tbl_Xuat.Rows[iXuat]["Thucpham_thucanId"].ToString().Trim())
                        {
                            Tbl_Nhap.Rows[iNhap]["SlXuat"] = Tbl_Xuat.Rows[iXuat]["SlXuat"];
                            Tbl_Nhap.Rows[iNhap]["GiaXuat"] = Tbl_Xuat.Rows[iXuat]["GiaXuat"];
                            Tbl_Xuat.Rows.RemoveAt(iXuat);
                            break;
                        }
                    }
                }
                // add toàn bộ dữ liệu tồn vào nhập
                for (int _iXuat = 0; _iXuat < Tbl_Xuat.Rows.Count; _iXuat++)
                {
                    DataRow NewRw = Tbl_Nhap.NewRow();
                    NewRw["STT"] = Tbl_Nhap.Rows.Count + 1;
                    NewRw["Thucpham_ThucanId"] = Tbl_Xuat.Rows[_iXuat]["Thucpham_thucanId"];
                    NewRw["SlXuat"] = Tbl_Xuat.Rows[_iXuat]["SlXuat"];
                    NewRw["Giaxuat"] = Tbl_Xuat.Rows[_iXuat]["Giaxuat"];
                    NewRw["Tenthucan"] = Tbl_Xuat.Rows[_iXuat]["Tenthucan"];
                    NewRw["Tendonvitinh"] = Tbl_Xuat.Rows[_iXuat]["Tendonvitinh"];
                    NewRw["SlNhap"] = 0;
                    NewRw["Gianhap"] = 0;
                    NewRw["SlTon"] = 0;
                    NewRw["Giaton"] = 0;
                    Tbl_Nhap.Rows.Add(NewRw);
                }

                // su ly ton vao nhap
                for (int iNhap = 0; iNhap < Tbl_Nhap.Rows.Count; iNhap++)
                {
                    for (int iTon = 0; iTon < Tbl_Ton.Rows.Count; iTon++)
                    {
                        string Nhap = Tbl_Nhap.Rows[iNhap]["tenthucan"].ToString();
                        string Xuat = Tbl_Ton.Rows[iTon]["tenthucan"].ToString();

                        if (Tbl_Nhap.Rows[iNhap]["Thucpham_thucanId"].ToString().Trim() == Tbl_Ton.Rows[iTon]["Thucpham_thucanId"].ToString().Trim())
                        {
                            Tbl_Nhap.Rows[iNhap]["SlTon"] = Tbl_Ton.Rows[iTon]["Slton"];
                            Tbl_Nhap.Rows[iNhap]["Giaton"] = Tbl_Ton.Rows[iTon]["Giaton"];
                            Tbl_Ton.Rows.RemoveAt(iTon);
                            break;
                        }

                    }
                }
                // add toàn bộ dữ liệu tồn vào nhập
                for (int _iTon = 0; _iTon < Tbl_Ton.Rows.Count; _iTon++)
                {
                    if (Tbl_Ton.Rows[_iTon]["Slton"].ToString() != "0")
                    {
                        DataRow NewRw = Tbl_Nhap.NewRow();
                        NewRw["Thucpham_ThucanId"] = Tbl_Ton.Rows[_iTon]["Thucpham_thucanId"];
                        NewRw["Slton"] = Tbl_Ton.Rows[_iTon]["Slton"];
                        NewRw["Giaton"] = Tbl_Ton.Rows[_iTon]["Giaton"];
                        NewRw["SlNhap"] = 0;
                        NewRw["Gianhap"] = 0;
                        NewRw["SlXuat"] = 0;
                        NewRw["GiaXuat"] = 0;
                        NewRw["Tenthucan"] = Tbl_Ton.Rows[_iTon]["Tenthucan"];
                        NewRw["Tendonvitinh"] = Tbl_Ton.Rows[_iTon]["Tendonvitinh"];
                        NewRw["STT"] = Tbl_Nhap.Rows.Count + 1;
                        Tbl_Nhap.Rows.Add(NewRw);
                    }
                }
            }
            catch
            {

            }

            return Tbl_Nhap;
        }



    }
}
